"use strict";
/* Controllers */
app.controller("zhaopinzheDimController", ['$scope', '$location', '$rootScope', '$routeParams', 'zhaopinzheDimService', '$q', function ($scope, $location, $rootScope, $routeParams, zhaopinzheDimService, $q) {
    var vm = $scope.vm = {};

    vm.p = {};
    vm.p.biz = 'zp';
    vm.p.dim = '1';
    vm.p.appDiv = 'zhaopinzhe';

    var datePicker = $('.date');
    datePicker.datetimepicker({
        locale: 'zh-cn',
        format: 'YYYY-MM-DD'
    });
    datePicker.on('dp.hide', function (e) {
        var currentInput = $(e.currentTarget).find('input');
        var eName = currentInput.attr('name');
        var p = $scope.vm.p;
        p[eName] = currentInput.val();
        $scope.$apply()
    });

    if ($scope.vm.p.bizTime == '' || typeof ($scope.vm.p.bizTime) == 'undefined') {
        $scope.vm.p.bizTime = new Date(new Date().getTime() - 86400000).format('yyyy-MM-dd');
    }

    var errorFn = function (result) {
        alert(result);
    };

    $scope.listDashboardDimData = function () {
        var params = {};
        params = vm.p;
        var procFn = function (response) {
            var data = response.data;
            if (data == null) {
                alert("请求失败：" + response.message);
                return;
            }
            $scope.vm.dashboardDimData = data
        };
        return zhaopinzheDimService.listDashboardDimData(params, procFn, errorFn);
    };

    $scope.initTables = function () {
        $scope.$apply();
        var dashboardDimData = $scope.vm.dashboardDimData;
        for(var i in dashboardDimData) {
            var item = dashboardDimData[i];
            var columns = [];
            for(var k in item.columnNames) {
                var element = item.columnNames[k];
                var column = {};
                column.title = element;
                column.field = element;
                column.sortable = true;
                columns.push(column)
            }
            columns[0].width=100;
            var tableData = item.tableData;
            var $table = $("#list_table_" + item.configId);
            $table.bootstrapTable('destroy').bootstrapTable({
                columns: columns,
                data: tableData
            })
        }
    };

    $scope.queryDashboard = function () {
        $("#refreshTable").addClass('disabled');
        $scope.listDashboardDimData();
    };

    $scope.queryDashboard()

}]);

//自定义指令实现ng-repeat完成后执行repeatFinish方法
//app.directive('myRepeatFinish', function () {
//    return {
//        restrict: 'A',
//        link: function (scope, element, attr) {
//            console.log(scope.$index);
//            if (scope.$last == true) {
//                console.log('ng-repeat over, will draw chart.');
//                scope.$eval(attr.myRepeatFinish)
//            }
//        }
//    }
//});

/* Services */
app.service('zhaopinzheDimService', ['ajaxService', function (ajaxService) {

    this.listCoreMetrics = function (params, successFunction, errorFunction) {
        return ajaxService.AjaxPost(params, "/star/api/basic-service/v1/web_dapan/listCoreMetrics.do", successFunction, errorFunction);
    };
    this.listChartMetrics = function (params, successFunction, errorFunction) {
        return ajaxService.AjaxPost(params, "/star/api/basic-service/v1/web_dapan/listAllMetricSeries.do", successFunction, errorFunction);
    };
    this.listDashboardDimData = function (params, successFunction, errorFunction) {
        return ajaxService.AjaxPost(params, "/star/api/basic-service/v1/web_dapan/listDashboardDimData.do", successFunction, errorFunction);
    };

}]);

